﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;

// add
using System.IO;
using System.Data;
using FormBaseLib;


namespace FormBase
{
    public class CProjectXML: CBase
    {
        DataSetProject mDataSet;
        bool mbNewRow;

        // default values:
        const string mcsCompanyName = "CompanyName";
        const string mcsSystemName = "SystemName";
        const string mcsProjectName = "ProjectName";

        public string msFile { get; private set; }
        DataSetProject.TProjectDataTable mTable;
        public DataSetProject.TProjectRow mRow { get; set; }
        public CProjectXML()
        {
            mDataSet = new DataSetProject();
            mbNewRow = false;
        }

        public bool Save()
        {
            ClearLastError();
            if (string.IsNullOrEmpty(msFile))
            {
                SetLastError("ERROR: Empty filename.");
                return false;
            }
            if (!Directory.Exists(Path.GetDirectoryName(msFile)))
            {
                SetLastError(string.Format("ERROR: Directory does not exist in file {0}.", msFile));
                return false;
            }

            //// Project info:
            //rowData.FEnvironment = (int)CProject.mRuntime;

            File.WriteAllText(msFile, mDataSet.GetXml(), Encoding.UTF8);
            return true;
        }
        public bool SaveSchema(string sFile)
        {
            File.WriteAllText(sFile, mDataSet.GetXmlSchema().Replace("utf-16", "utf-8"), Encoding.UTF8);
            return true;
        }

        public bool Load(string sFile)
        {
            msFile = sFile;
            if (string.IsNullOrWhiteSpace(msFile))
            {
                SetLastError(string.Format("ERROR: Empty file anme."));
                return false;
            }
            if (!File.Exists(msFile))
            {
                SetLastError(string.Format("ERROR: File does NOT exist. {0}.", msFile));
                return false;
            }
            mDataSet.Clear();

            XmlReadMode mode1 = mDataSet.ReadXml(msFile);
            //todo if (mode1 == XmlReadMode.Auto)
            //DataSetProject.TProjectDataTable t1;
            mTable = mDataSet.TProject;
            if (mTable.Rows.Count < 1)
            {
                SetLastError(String.Format("ERROR: Rows count < 1 in file {0}.", msFile));
                return false;
            }

            mRow = mTable.Rows[0] as DataSetProject.TProjectRow;
            mDataSet.AcceptChanges(); // make no changes!


            return true;
        }
        public void CreateNewRowDefault()
        {
            DataSetProject.TProjectDataTable t1;
            mDataSet.Clear();
            t1 = mDataSet.TProject;

            // default values, all fields MUST be included.
            mRow = t1.NewTProjectRow();
            mRow.FCompanyName = mcsCompanyName;
            mRow.FDebug = 0;
            mRow.FEnvironment = (int)enumEnvironment.Production;
            mRow.FID = CProject.mApp.msProjectID;
            mRow.FProjectName = mcsProjectName;
            mRow.FSystemName = mcsSystemName;
            t1.AddTProjectRow(mRow);

            mbNewRow = true;
        }

        public bool HasChanges()
        {
            if (mbNewRow)
                return true;

            return mDataSet.HasChanges();
        }
    }
}
